package com.puboot.module.blog.controller;

import com.puboot.common.util.*;
import com.puboot.module.admin.vo.base.ResponseVo;
import com.puboot.module.blog.model.*;
import com.puboot.module.blog.service.*;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/blog/plateglass/api")
@AllArgsConstructor
public class PlateGlassController {

    private final HttpServletRequest httpServletRequest;
    private final PlateGlassService plateGlassService;
    private final PlateGlassDetailService plateGlassDetailService;
    private final PlateGlassDecomposeService plateGlassDecomposeService;

    @PostMapping("/result")
    @Transactional
    public ResponseVo result(PlateGlass plateGlass) {
        try {
            HttpSession httpSession = httpServletRequest.getSession();
            Customer customer = (Customer) httpSession.getAttribute("customer");
            List<String> ncvs = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getNcv())) {
                ncvs = Arrays.asList(plateGlass.getNcv().split(","));
            }
            List<String> fcs = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getFc())) {
                fcs = Arrays.asList(plateGlass.getFc().split(","));
            }
            List<String> ccs = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getCci())) {
                ccs = Arrays.asList(plateGlass.getCci().split(","));
            }
            List<String> ofs = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getOf())) {
                ofs = Arrays.asList(plateGlass.getOf().split(","));
            }

            List<String> mfis = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getMfi())) {
                mfis = Arrays.asList(plateGlass.getMfi().split(","));
            }
            List<String> mis = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getMi())) {
                mis = Arrays.asList(plateGlass.getMi().split(","));
            }
            List<String> efis = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getEfi())) {
                efis = Arrays.asList(plateGlass.getEfi().split(","));
            }
            List<String> fis = new ArrayList<>();
            if (StringUtils.isNotBlank(plateGlass.getFi())) {
                fis = Arrays.asList(plateGlass.getFi().split(","));
            }

            Double eBurn = SpinUtil.getEBurn(ncvs, fcs, ccs, ofs);
            Double eToner = PlateGlassUtil.getEToner(plateGlass.getQc(), plateGlass.getCc());
            Double eDecompose = PlateGlassUtil.getEDecompose(mfis, mis, efis, fis);
            Double ePurchaseElectric = SpinUtil.getEPurchaseElectric(plateGlass.getAdPurchaseElectric(), plateGlass.getEfPurchaseElectric());
            Double ePurchaseHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(plateGlass.getAdPurchaseHeat());
            Double eOutputHeat = SpinUtil.getEPurchaseHeatOrEOutputHeat(plateGlass.getAdOutInputHeat());
            Double eOutputElectric = SpinUtil.getEOutputElectric(plateGlass.getAdOutInputElectric(), plateGlass.getEfOutInputElectric());
            Double result = PlateGlassUtil.getE(eBurn, eToner, eDecompose, ePurchaseElectric,
                    ePurchaseHeat, eOutputElectric, eOutputHeat);
            plateGlass.setCustomerId(customer.getId());
            plateGlass.setCreateTime(new Date());
            plateGlass.setEDecompose(GasDCUtil.round(eDecompose, 2));
            plateGlass.setEBurn(GasDCUtil.round(eBurn, 2));
            plateGlass.setEToner(GasDCUtil.round(eToner, 2));
            plateGlass.setEPurchaseElectric(GasDCUtil.round(ePurchaseElectric, 2));
            plateGlass.setEPurchaseHeat(GasDCUtil.round(ePurchaseHeat, 2));
            plateGlass.setEOutputHeat(GasDCUtil.round(eOutputHeat, 2));
            plateGlass.setEOutputElectric(GasDCUtil.round(eOutputElectric, 2));
            plateGlass.setState(Constants.STATE_INVAILD);
            plateGlass.setResult(GasDCUtil.getThousandth(result));
            plateGlassService.save(plateGlass);
            for (int i = 0; i < ncvs.size(); i++) {
                PlateGlassDetail plateGlassDetail = new PlateGlassDetail();
                plateGlassDetail.setNcv(Double.valueOf(ncvs.get(i)));
                plateGlassDetail.setFc(Double.valueOf(fcs.get(i)));
                plateGlassDetail.setCc(Double.valueOf(ccs.get(i)));
                plateGlassDetail.setOf(Double.valueOf(ofs.get(i)));
                plateGlassDetail.setPlateGlassId(plateGlass.getId());
                plateGlassDetailService.save(plateGlassDetail);
            }
            for (int i = 0; i < mfis.size(); i++) {
                PlateGlassDecompose plateGlassDecompose = new PlateGlassDecompose();
                plateGlassDecompose.setMfi(Double.valueOf(mfis.get(i)));
                plateGlassDecompose.setMi(Double.valueOf(mis.get(i)));
                plateGlassDecompose.setEfi(Double.valueOf(efis.get(i)));
                plateGlassDecompose.setFi(Double.valueOf(fis.get(i)));
                plateGlassDecompose.setPlateGlassId(plateGlass.getId());
                plateGlassDecomposeService.save(plateGlassDecompose);
            }
            return ResultUtil.success("计算成功", plateGlass);
        } catch (Exception e) {
            log.error("计算失败", e);
            return ResultUtil.error("计算失败");
        }
    }


}
